

package org.mechanic.core.web;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

@SuppressWarnings("serial")
public class HttpStatusCode implements Serializable {

    private static final long serialVersionUID = -3831880461596805413L;
    /**
     * 200请求成功
     */
    public static HttpStatusCode OK = new HttpStatusCode(200);
    /**
     * 用户或密码错误
     */
    public static HttpStatusCode ACCOUNT_PASSWORD_ERROR = new HttpStatusCode(30000);
    /**
     * 身份未认证
     */
    public static HttpStatusCode UNAUTHOR = new HttpStatusCode(30001);
    /**
     * 未登录
     */
    public static HttpStatusCode LOGIN_FAIL = new HttpStatusCode(30002);
    /**
     * 权限不足
     */
    public static HttpStatusCode FORBIDDEN = new HttpStatusCode(30003);
    /**
     * 频繁操作
     */
    public static HttpStatusCode MULTI_STATUS = new HttpStatusCode(30004);
    /**
     * 参数不能为空
     */
    public static HttpStatusCode PARAM_NOT_NULL = new HttpStatusCode(30010);
    /**
     * 参数格式不正确
     */
    public static HttpStatusCode PARAM_NOT_FORMAT = new HttpStatusCode(30011);
    /**
     * 非法参数
     */
    public static HttpStatusCode PARAM_ILLEGAL = new HttpStatusCode(30012);
    /**
     * 数据解析有误
     */
    public static HttpStatusCode DATA_PARSE = new HttpStatusCode(30013);
    /**
     * 参数值太小
     */
    public static HttpStatusCode PARAM_MIN_VALUE = new HttpStatusCode(30014);
    /**
     * 参数值太大
     */
    public static HttpStatusCode PARAM_MAX_VALUE = new HttpStatusCode(30015);
    /**
     * 参数长度过少
     */
    public static HttpStatusCode PARAM_MIN_LENGTH = new HttpStatusCode(30016);
    /**
     * 参数长度过长
     */
    public static HttpStatusCode PARAM_MAX_LENGTH = new HttpStatusCode(30017);
    /**
     * 无效用户
     */
    public static HttpStatusCode USER_INVALID = new HttpStatusCode(30020);
    /**
     * 用户状态错误
     */
    public static HttpStatusCode USER_LOCKED = new HttpStatusCode(30021);
    /**
     * 500服务器出错
     */
    public static HttpStatusCode INTERNAL_SERVER_ERROR = new HttpStatusCode(500);
    /**
     * 自定义错误编码
     */
    public static HttpStatusCode DEFINE_ERROR = new HttpStatusCode(500);
    /**
     * 没有客服
     */
    public static HttpStatusCode NON_KFUSER = new HttpStatusCode(60001);

    private Integer value;

    private String message;

    private Object[] valueObject = null;

    private HttpStatusCode(Integer value) {
        this.value = value;
    }

    private HttpStatusCode() {
    }

    private HttpStatusCode(Integer value, String message) {
        this.value = value;
        this.message = message;
    }

    public static HttpStatusCode paramFormat(HttpStatusCode statusCode, Object... objects) {
        statusCode = new HttpStatusCode(statusCode.value, statusCode.message);
        statusCode.valueObject = objects;
        return statusCode;
    }

    public static HttpStatusCode defineHttpStatusCode(Integer code, String message) {
        return new HttpStatusCode(code, message);
    }

    public static HttpStatusCode defineHttpStatusCode(HttpStatusCode statusCode, String message) {
        statusCode = new HttpStatusCode(statusCode.value, message);
        return statusCode;
    }

    public static HttpStatusCode defineHttpStatusCode(String message) {
        HttpStatusCode statusCode = new HttpStatusCode(500, message);
        statusCode.message = message;
        return statusCode;
    }

    public Integer value() {
        return this.value;
    }

    public String message() {
        if (StringUtils.isNoneBlank(message)) {
            return message;
        }
        return Resources.getMessage("CODE_" + this.value, valueObject);
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String toString() {
        return this.value.toString();
    }
}
